package com.demoo.zjsu.service.controllerservice;


import com.demoo.zjsu.string.BaseStrings;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by jshjsh on 2016/8/14.
 */
@Service
public class Parameter {

    private Object[] param;

    private Class dao;

    private String methodName;

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public Class getDao() {
        return dao;
    }

    public void setDao(Class dao) {
        this.dao = dao;
    }

    public Object[] getParam() {
        return param;
    }

    public void setParam(Object[] param) {
        this.param = param;
    }

    public Object invoke(){
        try {
            Class objectType = param.getClass();
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(BaseStrings.SPRING_FILE);
            Object object = context.getBean(dao);
            Method method = dao.getDeclaredMethod(methodName, objectType);
            return method.invoke(object,param);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }


}
